//
//  MessageComposeViewController.m
//  cliqset
//
//  Created by David Hinson on 4/3/09.
//  Copyright 2009 Cliqset, Inc.. All rights reserved.
//

#import "MessageComposeViewController.h"
#import "ChooseRecipientViewController.h"
#import "cliqsetAppDelegate.h"
#import "Message.h"
#import "Relationship.h"

@implementation MessageComposeViewController

@synthesize recipientField;
@synthesize subjectField;
@synthesize messageField;
@synthesize parent;
@synthesize index;
@synthesize cancelButton;
@synthesize sendButton;
@synthesize messageContent;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    [super viewDidLoad];
	
	self.title = @"Compose";

	self.cancelButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(doCancel:)] autorelease];
	self.navigationItem.leftBarButtonItem = cancelButton;

	//self.sendButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(doSend:)] autorelease];
	self.sendButton = [[[UIBarButtonItem alloc] initWithTitle:@"Send" style:UIBarButtonItemStyleBordered target:self action:@selector(doSend:)] autorelease];
	self.navigationItem.rightBarButtonItem = sendButton;
	
	
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

// Do Send
-(IBAction)doSend:(id)id {	
	cliqsetAppDelegate* myApp                = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];	
	
	if ([self.subjectField.text isEqualToString:@""]) {
		[myApp addTextToLog:@"Please Enter A Subject" withCaption:@"Cliqset"];
		[self.subjectField becomeFirstResponder];
		return;
	}
	
	NSMutableArray * recipients = [[NSMutableArray alloc] init];
	
	Relationship * relationship = (Relationship *)[myApp.relationships objectAtIndex:self.index];
	[recipients addObject:relationship.targetIdentifier];
	
	[myApp.cliqset sendMessage:self.subjectField.text message:self.messageField.text recipients:recipients];
	[recipients release];

	[myApp slideComposeOut];
}

// Do Cancel
-(IBAction)doCancel:(id)id {
	cliqsetAppDelegate* myApp                = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];	
	[myApp slideComposeOut];
}

#pragma mark ---- Table Stuff ----

-(void)reloadData {
	[self.messageContent reloadData];
}

// Number of table sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

// Return rows in table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 3;
}

// Set row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	switch (indexPath.row) {
		case 0:
			return 40;
			break;
		case 1:
			return 40;
			break;
		case 2:
			return 200;
			break;
	}
	
	return 40;
}

// Draw cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

	cliqsetAppDelegate* myApp   = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	Relationship * relationship = (Relationship *)[myApp.relationships objectAtIndex:self.index];
    
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
    [cell backgroundView].opaque=YES;
    [cell backgroundView].clearsContextBeforeDrawing;
   	[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
		
	UIColor* textColor     = [UIColor colorWithRed:62.0/255.0 green:62.0/255.0 blue:62.0/255.0 alpha:1.0];
	
	switch (indexPath.row) {
		case 0:
			self.recipientField = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, 240, 30)];
			self.recipientField.textColor = textColor;
			self.recipientField.font = [UIFont systemFontOfSize:16];
			self.recipientField.text = [NSString stringWithFormat:@"%@ %@", 
										relationship.target.givenName == nil ? @"" : relationship.target.givenName, 
										relationship.target.familyName == nil ? @"" : relationship.target.familyName];
			
			[cell addSubview:self.recipientField];
			
			UILabel * to     = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 70, 30)];
			to.font          = [UIFont boldSystemFontOfSize:16];
			to.textColor     = textColor;
			to.text          = @"To:";
			to.textAlignment = UITextAlignmentRight;
			[cell addSubview:to];
			[to release];
			
			break;
		case 1:
			self.subjectField   = [[UITextField alloc] initWithFrame:CGRectMake(80, 10, 240, 30)];
			self.subjectField.textColor = textColor;
			self.subjectField.font = [UIFont systemFontOfSize:16];
			self.subjectField.autocorrectionType     = UITextAutocorrectionTypeNo;
			self.subjectField.autocapitalizationType = UITextAutocapitalizationTypeWords;
			
			[self.subjectField becomeFirstResponder];
			[cell addSubview:self.subjectField];
			
			UILabel * subj   = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 70, 30)];
			subj.font          = [UIFont boldSystemFontOfSize:16];
			subj.textColor     = textColor;
			subj.text          = @"Subject:";
			subj.textAlignment = UITextAlignmentRight;
			[cell addSubview:subj];
			[subj release];
			
			break;
		case 2:
			self.messageField   = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 310, 160)];
			//self.messageField.text = @"\r\n\nSent from Cliqset iPhone App";
			self.messageField.textColor = textColor;
			self.messageField.font = [UIFont systemFontOfSize:16];
			[cell addSubview:self.messageField];
			break;
	}

	return cell;
}

- (void)dealloc {
	[messageContent release];
	[sendButton release];
	[cancelButton release];
	[parent release];
	[recipientField release];
	[subjectField release];
	[messageField release];
    [super dealloc];
}


@end
